﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using DcsGauges.Shared.DataStructures;
using DcsGauges.Shared.Packets;
using System.Net;

namespace DcsGauges.Testing.GameSimulator
{
    public partial class Form1 : Form
    {
        private UdpClient                   client              = new UdpClient();
        private Encoding                    encoding            = Encoding.UTF8;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            return; //UDP doesn't require connect anymore

            if(this.client == null)
            {
                try
                {
                    this.client                     = new UdpClient(30335);
                    this.btnConnect.Text            = "Disconnect";
                }
                catch(Exception exc)
                {
                    MessageBox.Show(exc.ToString());
                }
            }
            else
            {
                this.client.Close();
                this.client = null;
                this.btnConnect.Text = "Connect";
            }
        }

        private void btnSendHandshake_Click(object sender, EventArgs e)
        {
            string packetContent    = "<hs>DcsA10;SimulatedGameClient</hs>";
            byte[] toSend           = this.encoding.GetBytes(packetContent);
            IPEndPoint sendto       = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30334);
            this.client.Send(toSend, toSend.Length, sendto);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (int propId in DcsProperties.AllProperties)
            {
                DcsProperty p = new DcsProperty(propId);
                this.cbDataProperty.Items.Add(p.Name);
            }

            this.cbDataProperty.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DcsProperty prop                   = new DcsProperty(this.cbDataProperty.Text);
            DataPacket packet                   = new DataPacket(prop, (float)this.nudValue.Value);
            PacketBuilder builder               = new PacketBuilder();
            byte[] buffer                       = builder.Build(packet);

            IPEndPoint sendto       = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30334);
            this.client.Send(buffer, buffer.Length, sendto);
        }
    }
}
